$(function(){
	var $shelfTable = $('#shelfTable');
	var $btnRefresh = $('#btnRefresh');
	var $exportDate = $('#exportDate');
	var $btnExport = $('#btnExport');
	
	initData();
	initShelfTable();
	initListener();
	
	function initData(){
		$.ajax({
			url: "hashrates/latest",
			type: "GET",
			contentType: "application/json; charset=utf-8",
			success: function(result){
				if(result.success){
					var mine = result.data;
					$('#detectTime').text(mine.detectTimeDisplay+'（'+mine.timeZoneDisplay+'）');
					$('#numAll').text(mine.numAll);
					$('#numDetail').text(mine.numDetail);
					$('#numPassed').text(mine.numPassed);
					$('#numUnpassed').text(mine.numUnpassed);
					$('#numFailure').text(mine.numFailure);
					$('#ratePassed').text(mine.ratePassedD);
					$('#overallHashrate').text(mine.overallRealD)
					$shelfTable.bootstrapTable('load', mine.shelfOverviews);
				}
			}
		});
	}
	
	function initListener() {
		$btnRefresh.on('click', function() {
			initData();
		});
	}
	function initShelfTable(){
		$shelfTable.bootstrapTable({
			showRefresh: true,
			showToggle: true,
			showColumns: true,
			search: true,
			columns:[
			{
				field: 'minerShelfName',
				title: '机架名称',
				align: 'center'
			},
			{
				field: 'numAll',
				title: '矿机总数',
				align: 'center'
			},
			{
				field: 'numDetail',
				title: '检测到的矿机数',
				align: 'center'
			},
			{
				field: 'numFailure',
				title: '未检测到的矿机数',
				align: 'center',
				cellStyle: function(value, row, index){
					if(value>0){
						return {classes:'warning'};
					}
					return {};
				}
			},
			{
				field: 'numPassed',
				title: '检测合格的矿机数',
				align: 'center'
			},
			{
				field: 'numUnpassed',
				title: '检测不合格的矿机数',
				align: 'center',
				cellStyle: function(value, row, index){
					if(value>0){
						return {classes:'warning'};
					}
					return {};
				}
			},
			{
				field: 'ratePassed',
				title: '算力合格率',
				align: 'center',
				formatter: function(value, row, index){
					return formatMoney((value*100),2)+'%';
				}
			},
			{
				field: 'overallReal',
				title: '机架实际算力(G)',
				align: 'center',
				formatter: function(value, row, index){
					return formatMoney(value, 2);
				}
			},
			{
				field: 'overallTheory',
				title: '机架理论算力(G)',
				align: 'center',
				formatter: function(value, row, index){
					return formatMoney(value, 2);
				}
			},
			{
				field: 'operate',
				title: '操作',
				align: 'center',
				formatter: function(value, row, index){
					var dataUrl = '\'overview/detail?sid=' + row.minerShelfId + '&dtime=' + row.detectTime + '\'';
					var menuName = '\'' + row.minerShelfName + '\'';
					return '<a onclick="window.parent.openTab(event, ' + dataUrl + ', ' + menuName + ')">详情</a>';
				}
			}]
		});
	}
})